package com.fosun.spr.core;

import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.fosun.spr.core.base.model.CoBaseSpModel;
import com.fosun.spr.core.base.model.CoBaseSrModel;
import com.fosun.spr.core.exception.ServiceException;

@Component
@Aspect
public class WebControllerAop {

	protected Logger logger = Logger.getLogger(WebControllerAop.class);

	// @Pointcut("execution(* com.fosun.spr.plugin.uuc..*.*(..))")
	// public void executeService() {
	// System.out.println("------------");
	// }

	// @Before("executeService()")
	// public void doBeforeAdvice(JoinPoint joinPoint) {
	// //System.out.println("我是前置通知!!!");
	//
	// }

	@Around("execution(* com..*ServiceImpl.*(..))")
	public Object doAroundAdviceService(ProceedingJoinPoint proceedingJoinPoint) {
		MethodSignature ms = (MethodSignature) proceedingJoinPoint.getSignature();
		Class returnClassType = ms.getReturnType();
//		logger.info(proceedingJoinPoint.getTarget().getClass().getSimpleName() + "#"
//				+ proceedingJoinPoint.getSignature().getName() + " Begin");
		CoBaseSrModel srModel = null;
		try {
			srModel = (CoBaseSrModel) proceedingJoinPoint.proceed();
		} catch (ServiceException e) {
			e.printStackTrace();
			try {
				srModel = (CoBaseSrModel) returnClassType.newInstance();
				srModel.setErrcode(e.getCode());
				srModel.setErrmsg(e.getMessage());
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		} catch (Throwable e) {
			e.printStackTrace();
			try {
				srModel = (CoBaseSrModel) returnClassType.newInstance();
				srModel.setErrcode("-1");
				srModel.setErrmsg(e.getMessage());
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}

//		long costMs = System.currentTimeMillis() - beginTime;
//		logger.info(proceedingJoinPoint.getTarget().getClass().getSimpleName() + "#"
//				+ proceedingJoinPoint.getSignature().getName() + "(" + costMs + " ms) End!\r\n");
		return srModel;
	}
	
	@Around("execution(* com..*Control.*(..))")
	public Object doAroundAdviceControl(ProceedingJoinPoint proceedingJoinPoint) {
		long beginTime = System.currentTimeMillis();
		StringBuffer sb = new StringBuffer(proceedingJoinPoint.getTarget().getClass().getSimpleName() + "#"
				+ proceedingJoinPoint.getSignature().getName() + " Begin\r\n");
		Object[] args = proceedingJoinPoint.getArgs();
		for(int i=0,j=args.length;i<j;i++) {
			//sb.append(args[i].getClass().getSimpleName());
			if(args[i] instanceof CoBaseSpModel) {
				String spjson = JSON.toJSONString(args[i]); 
				sb.append(spjson+"");
			}	
		}
		logger.info(sb);
 
		
		
		Object srModel = null;
		try {
			srModel = (CoBaseSrModel) proceedingJoinPoint.proceed();
		} catch (Throwable e) {
			e.printStackTrace();
		}
		
		String srjson = JSON.toJSONString(srModel); 
		long costMs = System.currentTimeMillis() - beginTime;
		logger.info(proceedingJoinPoint.getTarget().getClass().getSimpleName() + "#"
				+ proceedingJoinPoint.getSignature().getName() + " End(" + costMs + " ms)\r\n" +srjson+"\r\n\r\n");
		 
		
		return srModel;
	}	
}
